
package com.leo.util.tcpclient;

import java.net.InetSocketAddress;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



public class TcpClient{
	public static void main(String[] args) {
		int numTasks = 1;

		ExecutorService exec = Executors.newCachedThreadPool();
		for (int i = 0; i < numTasks; i++) {
			exec.execute(createTask(i));
		}

	}

	// 定义一个简单的任务
	private static Runnable createTask(final int taskID) {
		
		return new Runnable() {
			private Socket socket = null;

			public void run() {
				try {
					System.out.println("Task " + taskID + ":start");
					long startTime = System.currentTimeMillis();   //获取开始时间
					String ip = "192.168.5.88";
//					String ip = "192.168.5.76";
//					String ip = "183.63.53.103";
//					String ip = "192.168.5.75";
					socket = new Socket(ip, 20618);
					//socket.setSoTimeout(5000);
					// 发送关闭命令
					System.out.println("IP ---------" + socket.getLocalSocketAddress());
					OutputStream socketOut = socket.getOutputStream();
					
					int lenth = 2396;
					ByteBuffer byteBuffer = ByteBuffer.allocate(188);

					ByteBuffer binVer = ByteBuffer.allocate(4);
					binVer.put("v303".getBytes());

					ByteBuffer custId = ByteBuffer.allocate(32);
					custId.put("CS00010001".getBytes());

					ByteBuffer configNo = ByteBuffer.allocate(16);
					//configNo.put("A20131218123240".getBytes());
					configNo.put("00de1eba1111".getBytes());

					ByteBuffer fileNo = ByteBuffer.allocate(16);
					//fileNo.put("A20131218123240".getBytes());
					fileNo.put("C0000000000002".getBytes());

					ByteBuffer androidId = ByteBuffer.allocate(32);
					androidId.put("c7c69a8c665b9fd5".getBytes());

					ByteBuffer imsi = ByteBuffer.allocate(32);
					imsi.put("460020000000000".getBytes());

					ByteBuffer plat = ByteBuffer.allocate(16);
					plat.put("MT8888".getBytes());

					ByteBuffer defaultStr = ByteBuffer.allocate(16);
					defaultStr.put("11".getBytes());

					ByteBuffer defaultStr2 = ByteBuffer.allocate(16);
					defaultStr2.put("863766020065466".getBytes());

//					int notProssLenth = 2208;
//					String res = "com.tydtech.weather&com.tyd.calculator&com.mediatek.voiceunlock&com.mediatek.lbs.em&com.android.htmlviewer&com.kingroot.kinguser&com.android.bluetooth&com.mediatek.voicecommand&com.tydtech.onlinetheme&com.android.calendar&com.mediatek.mtklogger&com.android.launcher3&com.android.providers.downloads.ui&com.android.documentsui&com.mediatek.filemanager&com.android.provision&com.android.certinstaller&com.mediatek.bluetooth&com.mediatek.batterywarning&com.fw.upgrade.sysoper&com.tydtech.facewake&com.fw.upgrade&com.mediatek.thermalmanager&com.mediatek.calendarimporter&com.android.gallery3d&com.android.providers.drm&com.tydtech.widget.moodalbum&com.android.exchange&com.android.wallpaper.livepicker&com.android.packageinstaller&com.android.providers.telephony&com.mediatek.schpwronoff&com.mediatek.CellConnService&com.svox.pico&com.android.email&com.tydtech.motionrecognition&com.tydtech.floattask&com.tydtech.music&com.tydtech.videogallery&com.tydtech.tydweatherwidget&com.mediatek.FMRadio&com.android.phasebeam&com.android.soundrecorder&com.tydtech.thememanager&com.android.inputmethod.latin&com.mediatek.omacp&com.mediatek.factorymode&com.sohu.inputmethod.sogou&com.mcube.magcal&com.mediatek.GoogleOta&com.tydtech.tydswitchboot&com.android.stk&com.mediatek.googleota.sysoper&com.mcube.acc&com.android.providers.userdictionary&com.tydtech.hotknot&com.mediatek.apst.target&com.mediatek.systemupdate&com.android.pacprocessor&com.android.galaxy4&com.tydtech.locknow&com.tydtech.tydsearch.activity&com.android.printspooler&com.tydtech.scenelauncher&com.android.protips&com.mediatek.hotknot.service&com.android.providers.applications&com.android.dreams.basic&com.android.simmelock&com.android.keychain&com.android.wallpaper&com.tydtech.wallpaperchooser&com.mediatek.engineermode&com.android.deskclock&com.mediatek.systemupdate.sysoper&com.tydtech.musicpage&com.mediatek.appguide.plugin&com.tydtech.thememanager.core&com.mediatek.StkSelection&com.mediatek.ygps&com.tydtech.tydcrontab:com.tencent.mm&com.qugouinc.webapp&com.tencent.news&com.baidu.BaiduMap&com.tydtech.theme.forest_960x540_v300&com.tudou.android&com.tencent.mobileqq&com.kkw.icon&com.tydtech.theme.miui_960x540_v300&com.dianxinos.optimizer.channel:";
//					int notProssLenth = 122;
//					String res = "N=A1505051019013&B15050510190135&J1505051019013&R1504271416024&M1504271416024|D=169&783&1305|W=wifi|E=1005|M=hongmi&XY-A6";
					int notProssLenth = 0;
					String res = "";
					ByteBuffer notPross = ByteBuffer.allocate(notProssLenth);
					notPross.put(res.getBytes());
					
					byteBuffer.put(binVer.array());
					byteBuffer.putInt(0);
					byteBuffer.put(custId.array());
					byteBuffer.put(configNo.array());
					byteBuffer.put(fileNo.array());
					byteBuffer.put(androidId.array());
					byteBuffer.put(imsi.array());
					byteBuffer.put(plat.array());
					byteBuffer.put(defaultStr.array());
					byteBuffer.put(defaultStr2.array());
					byteBuffer.putInt(Integer.reverseBytes(notProssLenth));
					byteBuffer.put(notPross.array());
					byteBuffer.rewind();
					socketOut.write(byteBuffer.array());
					socketOut.flush();
					
					///Thread.sleep(6000);
					// 接收服务器的反馈
					InputStream inputStream = socket.getInputStream();
					
					int total = 152;
					byte[] rea = new byte[total]; // 客户端请求头
					int recvCount = 0; // 已经成功读取的字节的个数
					
					// 循环读缓冲区域数据，直到读完位置
					while (recvCount < total) {
						recvCount += inputStream.read(rea, recvCount, total-recvCount);
					}
					//System.out.println(new String(rea));
					
					int baoliu = 0;
					int fileLength = 0;
					int recvLength = 0;
					int isLock = 0;
					String confNo = "";
					String netNum = "";
					String wifi = "";
					String configContext = "";
					
					ByteBuffer buffer = ByteBuffer.allocate(4);
					buffer.put(rea, 0, 4);
					buffer.rewind();
					baoliu = Integer.reverseBytes(buffer.getInt());
					buffer.flip();
					
					ByteBuffer buffer2 = ByteBuffer.allocate(4);
					buffer2.put(rea, 4, 4);
					buffer2.rewind();
					fileLength = Integer.reverseBytes(buffer2.getInt());
					buffer2.flip();
					
					ByteBuffer buffer3 = ByteBuffer.allocate(4);
					buffer3.put(rea, 8, 4);
					buffer3.rewind();
					recvLength = Integer.reverseBytes(buffer3.getInt());
					buffer2.flip();
					
					ByteBuffer buffer4 = ByteBuffer.allocate(4);
					buffer4.put(rea, 12, 4);
					buffer4.rewind();
					isLock = Integer.reverseBytes(buffer4.getInt());
					buffer4.flip();
					
					confNo = new String(rea, 16, 16).trim();
					if(null == confNo || "".equals(confNo)){
						confNo = "C00000000001";
					}
					//netNum = new String(rea, 32, 4).trim();
					netNum = new String(rea, 32, 120).trim();
					//wifi = new String(rea, 36, 4).trim();
					
					if(recvLength != 0){
						byte[] byteArr = new byte[recvLength]; // 客户端请求头
						int readCount = 0; // 已经成功读取的字节的个数
						// 循环读缓冲区域数据，直到读完位置
						while (readCount < recvLength) {
							readCount += inputStream.read(byteArr, readCount, recvLength-readCount);
							System.out.println("读取：" + readCount);
						}
						configContext = new String(byteArr, 0, recvLength).trim();
						System.out.println("总长：" + recvLength);
					}
					
					System.out.println("-------baoliu:" + baoliu + ", fileLength:" + fileLength + ", recvLength:" +recvLength +"," + " isLock:" +isLock + ", confNo:" + confNo + ",netNum:" + netNum + ", configContext:"+ configContext);
					
					long endTime = System.currentTimeMillis(); //获取结束时间

					System.out.println("程序运行时间： "+(endTime-startTime)/1000f+"秒");
				} catch (IOException ioEx) {
					ioEx.printStackTrace();
					try {
						if (socket != null) {
							socket.close();
						}
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}
				} catch (Exception e) {
					e.printStackTrace();
					try {
						if (socket != null) {
							socket.close();
						}
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}
				} finally {
					try {
						if (socket != null) {
							socket.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		};
	}
}
